App({
  globalData: {
    userInfo: null,
    hasLogin: false,
    sleepData: null,
    YMDevice:null,
  },
  
  onLaunch: function() {
    // 微信静默登录
    this.silentLogin();
  },
  
  silentLogin: function() {
    const that = this;
    // 获取登录凭证
    wx.login({
      success: res => {
        if (res.code) {
          // 发送 res.code 到后端换取 openId, sessionKey, unionId
          // 这里模拟登录成功
          console.log('登录成功，获取到code:', res.code);
          that.globalData.hasLogin = true;
          
          // 获取用户信息
          wx.getSetting({
            success: settingRes => {
              if (settingRes.authSetting['scope.userInfo']) {
                // 已经授权，可以直接调用 getUserInfo 获取头像昵称
                wx.getUserInfo({
                  success: userRes => {
                    that.globalData.userInfo = userRes.userInfo;
                    // 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
                    // 所以此处加入 callback 以防止这种情况
                    if (that.userInfoReadyCallback) {
                      that.userInfoReadyCallback(userRes);
                    }
                  }
                });
              }
            }
          });
        } else {
          console.log('登录失败！' + res.errMsg);
        }
      }
    });
  },

  updateGlobalData: function(data) {
    // 更新全局数据
    Object.assign(this.globalData, data);
    
    // 通知所有页面数据已更新
    const pages = getCurrentPages();
    pages.forEach((page) => {
      if (typeof page.onGlobalDataUpdate === 'function') {
        page.onGlobalDataUpdate();
      }
    });
  },

  fetchSleepData: function() {
    // 这里应该是从服务器获取睡眠数据的逻辑
    // 现在我们使用模拟数据
    const mockSleepData = {
      dailyData: {
        quality: 85,
        duration: '7小时30分钟',
        deepSleepDuration: '1小时52分钟',
        lightSleepDuration: '4小时8分钟',
        remSleepDuration: '1小时30分钟',
        fallAsleepTime: '23:30',
        wakeUpTime: '07:00',
        sleepScore: 85
      },
      sleepStages: [
        { stage: 'awake', startTime: '23:00', duration: 30 },
        { stage: 'light', startTime: '23:30', duration: 60 },
        { stage: 'deep', startTime: '00:30', duration: 90 },
        { stage: 'light', startTime: '02:00', duration: 60 },
        { stage: 'rem', startTime: '03:00', duration: 30 },
        { stage: 'light', startTime: '03:30', duration: 60 },
        { stage: 'deep', startTime: '04:30', duration: 30 },
        { stage: 'light', startTime: '05:00', duration: 90 },
        { stage: 'rem', startTime: '06:30', duration: 30 }
      ],
      weeklyData: [
        { day: '周一', quality: 75, duration: '7小时10分钟' },
        { day: '周二', quality: 80, duration: '7小时20分钟' },
        { day: '周三', quality: 70, duration: '6小时50分钟' },
        { day: '周四', quality: 85, duration: '7小时30分钟' },
        { day: '周五', quality: 82, duration: '7小时15分钟' },
        { day: '周六', quality: 88, duration: '8小时00分钟' },
        { day: '周日', quality: 85, duration: '7小时30分钟' }
      ]
    };

    // 更新全局数据
    this.updateGlobalData({ sleepData: mockSleepData });

    // 保存到本地存储
    wx.setStorageSync('sleepData', mockSleepData);
  }
});
